package com.controller;

import com.pojo.Emp;
import com.services.EmpService;
import com.utils.MyDataConverter;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

@WebServlet("/emp/*")
public class EmpServlet extends BaseServlet {
    public void index(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        EmpService es = new EmpService();
        ArrayList<Emp> list = es.getAll();
        req.getSession().setAttribute("EMP_LIST", list);
//        req.getRequestDispatcher("/empManage.jsp").forward(req,resp);
        resp.sendRedirect(req.getContextPath() + "/empManage.jsp");
    }

    public void addEmp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        Map<String, String[]> map = req.getParameterMap();
        Emp emp = new Emp();
        MyDataConverter mdc = new MyDataConverter();
        mdc.register(emp,map);

        EmpService es = new EmpService();
        es.add(emp);
        resp.sendRedirect("emp/index");
    }

    public void deleteEmp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        EmpService es = new EmpService();
        es.remove(id);
        resp.sendRedirect("emp/index");
    }

    //数据回写
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        EmpService es = new EmpService();
        Emp e = es.getEmp(id);
        req.getSession().setAttribute("EMP", e);
        resp.sendRedirect(req.getContextPath() + "/empModify.jsp");
    }

    //数据持久化到数据库
    public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        Map<String, String[]> map = req.getParameterMap();
        Emp emp = new Emp();
        try {
            BeanUtils.populate(emp, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        EmpService es = new EmpService();
        es.setEmp(emp);
        resp.sendRedirect("emp/index");
    }

    public void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String search = req.getParameter("search");
        EmpService es = new EmpService();
        ArrayList<Emp> list = es.getLike(search);
        req.getSession().setAttribute("EMP_LIST", list);
        resp.sendRedirect(req.getContextPath() + "/empManage.jsp");
    }

    public void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        EmpService es = new EmpService();
        int pages = (int) Math.ceil(es.getPage()/(1.0*5));
        req.getSession().setAttribute("EMP_ALL_PAGE",pages);
        req.getSession().setAttribute("EMP_NOW_PAGE",1);
        resp.sendRedirect("emp/index");
    }

    public void pagination(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pages = (int) req.getSession().getAttribute("EMP_ALL_PAGE");
        String page = req.getParameter("page");
        int nowPage = Integer.parseInt(page);
        if (nowPage < 1) {
            nowPage = 1;
        }else if (nowPage > pages){
            nowPage = pages;
        }
        req.getSession().setAttribute("EMP_NOW_PAGE",nowPage);
        EmpService es = new EmpService();
        ArrayList<Emp> list = es.getAll(nowPage);
        req.getSession().setAttribute("EMP_LIST", list);
//        req.getRequestDispatcher("/empManage.jsp").forward(req,resp);
        resp.sendRedirect(req.getContextPath() + "/empManage.jsp");
    }
}
